﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarchEstate.Manager.BusinesEntity;
using Newtonsoft.Json;
using System.Collections;
using CoreFrame.Business;
using System.Data;


public class SysInitController : AbstractJqueryController
{
    public void GetAreaList()
    {
        AbstractBaseData basedate = NewObject<AbstractBaseData>();
        TxtJson = JavaScriptConvert.SerializeObject(basedate.GetAreaList());
    }

    public void GetBuildList()
    {
        string areacode = ParamsData["areacode"];
        AbstractBaseData basedate = NewObject<AbstractBaseData>();
        TxtJson = JavaScriptConvert.SerializeObject(basedate.GetBuildList(areacode));
    }

    public void GetRoomList()
    {
        Hashtable hash = new Hashtable();
        hash.Add("areacode", ParamsData["areacode"]);
        hash.Add("buildcode", ParamsData["buildcode"]);
        hash.Add("roomname", ParamsData["roomname"]);

        int currpagenum = Convert.ToInt32(ParamsData["page"]);
        int pagesize = Convert.ToInt32(ParamsData["rows"]);
        CoreFrame.Core.Orm.PageInfo page = new CoreFrame.Core.Orm.PageInfo(pagesize, currpagenum);


        AbstractGenericModel model = NewObject<AbstractGenericModel>("sysinit");

        DataTable dt = (DataTable)model.OperationDataModel("GetRoomList", hash, page);
        TxtJson = base.DataTableToJson(dt, page.totalRecord);
    }

    public void GetShopList()
    {
        string shopname = ParamsData["shopname"];
        int currpagenum = Convert.ToInt32(ParamsData["page"]);
        int pagesize = Convert.ToInt32(ParamsData["rows"]);
        CoreFrame.Core.Orm.PageInfo page = new CoreFrame.Core.Orm.PageInfo(pagesize, currpagenum);
        AbstractGenericModel model = NewObject<AbstractGenericModel>("sysinit");

        DataTable dt = (DataTable)model.OperationDataModel("GetShopList", shopname, page);
        TxtJson = base.DataTableToJson(dt, page.totalRecord);
    }

    public void BatchBindingItem()
    {
        try
        {
            Hashtable hash = new Hashtable();
            hash.Add("type", ParamsData["type"]);
            hash.Add("binditemId", ParamsData["binditemId"]);
            hash.Add("delitem", ParamsData["delitem"]);
            hash.Add("itemcode", ParamsData["itemcode"]);
            hash.Add("ownerId", ParamsData["ownerId"]);
            AbstractGenericModel model = NewObject<AbstractGenericModel>("sysinit");

            model.OperationDataModel("BatchBindingItem", hash);
            TxtJson = ReturnSuccess("绑定收费项目成功！");
        }
        catch
        {
            TxtJson = ReturnError("操作失败！");
        }
    }

    public void BatchBingingMeter()
    {
        try
        {
            Hashtable hash = new Hashtable();
            hash.Add("type", ParamsData["type"]);
            hash.Add("ownerId", ParamsData["ownerId"]);
            hash.Add("ownerName", ParamsData["ownerName"]);
            hash.Add("srccId", ParamsData["srccId"]);
            hash.Add("srccName", ParamsData["srccName"]);

            hash.Add("formtype", ParamsData["formtype"]);
            hash.Add("formtype2", ParamsData["formtype2"]);
            hash.Add("metercount", ParamsData["metercount"]);
            hash.Add("itemcode", ParamsData["itemcode"]);
            
            AbstractGenericModel model = NewObject<AbstractGenericModel>("sysinit");

            model.OperationDataModel("BatchBingingMeter", hash);
            TxtJson = ReturnSuccess("绑定用表成功！");
        }
        catch
        {
            TxtJson = ReturnError("操作失败！");
        }
    }

    public void GetItemRelationList()
    {
        string srccId = ParamsData["srccId"];

        AbstractOSRCCItemRelation srcc = NewObject<AbstractOSRCCItemRelation>();
        TxtJson = ListToJson(srcc.GetSrccItemList(srccId));
    }

    public void UpdateBindingItem()
    {
        string data = ParamsData["data"];
        string[] rows = data.Split(new char[] { ',' });
        AbstractOSRCCItemRelation srcc = NewObject<AbstractOSRCCItemRelation>();
        for (int i = 0; i < rows.Length; i++)
        {
            string[] fields = rows[i].Split(new char[] { '#' });
            srcc.UpdateThisFee(fields[0], fields[1], Convert.ToDateTime(fields[2]), Convert.ToDateTime(fields[2]), Convert.ToDecimal(fields[4]), Convert.ToDecimal(fields[5]));
        }
        TxtJson = ReturnSuccess("保存成功！");
    }

    public void DeleteBindingItem()
    {
        string srccId = ParamsData["srccId"];
        string itemcode = ParamsData["itemcode"];
        AbstractOSRCCItemRelation srcc = NewObject<AbstractOSRCCItemRelation>();
        srcc.Delete(srccId, itemcode);
        TxtJson = ReturnSuccess("删除成功！");
    }

    public void GetCbServiceItemData()
    {
        AbstractServiceItem serviceitem = NewObject<AbstractServiceItem>();
        TxtJson = base.ModelToJson(serviceitem.GetServiceItemList("", "1"));
    }

    public void GetMakerList()
    {
        int meterType = Convert.ToInt32(ParamsData["formtype"]);
        int meterType2 = Convert.ToInt32(ParamsData["formtype2"]);
        int currpagenum = Convert.ToInt32(ParamsData["page"]);
        int pagesize = Convert.ToInt32(ParamsData["rows"]);
        CoreFrame.Core.Orm.PageInfo page = new CoreFrame.Core.Orm.PageInfo(pagesize, currpagenum);
        AbstractMeter meter = NewObject<AbstractMeter>();
        List<AbstractMeter> meterlist = meter.GetAllMeterList(meterType, meterType2, page);
        TxtJson = base.ListToJson(meterlist, page.totalRecord);
    }

    public void GetSrccMakerList()
    {
        string srccId = ParamsData["srccId"];
        int currpagenum = Convert.ToInt32(ParamsData["page"]);
        int pagesize = Convert.ToInt32(ParamsData["rows"]);
        CoreFrame.Core.Orm.PageInfo page = new CoreFrame.Core.Orm.PageInfo(pagesize, currpagenum);
        AbstractMeter meter = NewObject<AbstractMeter>();
        List<AbstractMeter> meterlist = meter.GetAllMeterList(srccId, page);
        TxtJson = base.ListToJson(meterlist, page.totalRecord);
    }

    public void DeleteMeter()
    {
        string meterId = ParamsData["meterId"];
        AbstractMeter meter = NewObject<AbstractMeter>();
        meter.Delete(meterId);
        TxtJson = ReturnSuccess("删除成功！");
    }

    public void UpdateMeter()
    {
        string data = ParamsData["data"];
        string[] rows = data.Split(new char[] { ',' });
        AbstractMeter meter = NewObject<AbstractMeter>();
        for (int i = 0; i < rows.Length; i++)
        {
            string[] fields = rows[i].Split(new char[] { '#' });
            meter.UpdateMeterInitNum(fields[0], Convert.ToDecimal(fields[1]));
        }
        TxtJson = ReturnSuccess("保存成功！");
    }
}
 
